const domainName = getApp().globalData.domainName
const header = {'content-type':'application/x-www-form-urlencoded'}
const dataType = 'json'
const method = 'POST'
const responseType = 'text'
// pages/my_recruit/my_recruit.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    docks:[
      {name:'待审核',status:2},
      {name:'已拒绝',status:3},
      {name:'已发布',status:4},
      {name:'已关闭',status:5},
    ],
    recruit_list:[],
    page_no:0,
  },

  // 查看招聘信息详情
  view_details(e){
    let index = e.currentTarget.id
    let recruitment_id = this.data.recruit_list[index].recruitment_id
    wx.navigateTo({
      url: '/pages/recruitment/recruitment?recruitment_id=' + recruitment_id,
    })
  },


  // 创建招聘信息
  create_recruit(){
    getApp().get_vibration()

    wx.navigateTo({
      url: '/pages/recruit_publish_admin/recruit_publish_admin?recruitment_info=' + JSON.stringify([]),
    })
  },

  create(){
    console.log('create')
  },

  // 选择标签
  sel_label(e){
    // getApp().get_vibration()
    let index = e.detail.index

    let status = this.data.docks[index].status
    // let status = e.currentTarget.id

    let page_no = 0
    this.setData({
      no_more:false,
      page_no:page_no,
      status:status,
    })
    this.load_recruit_list(status,page_no,false)
  },
  
  // 加载招聘列表
  load_recruit_list(status,page_no,load_more){
    wx.showLoading()
    wx.request({
      url: domainName + '/load_recruit_by_admin',
      data: {
        status:status,
        page_no:page_no,
      },
      dataType: dataType,
      enableCache: true,
      enableHttp2: true,
      enableQuic: true,
      header: header,
      method: method,
      responseType: responseType,
      timeout: 0,
      success: res => {
        wx.hideLoading()
        let recruit_list = res.data.recruit_list
        if(recruit_list.length>0){
          if(load_more){
            this.setData({
              recruit_list:this.data.recruit_list.concat(recruit_list),
              page_no:this.data.page_no + 1,
            })
          }else{
            this.setData({
              recruit_list:recruit_list,
              page_no:1,
              can_onshow:true,
            })
          }
        }else{
          if(!load_more){
            this.setData({
              page_no:0,
              recruit_list:[],
            })
          }
          this.setData({
            no_more:true,
          })
        }
      },
    })
  },

  // 查询我发布的招聘信息（待审核的）
  uncheck_recruit_count(){
    wx.request({
      url: domainName + '/uncheck_recruit_count',
      data: {},
      dataType: dataType,
      enableCache: true,
      enableHttp2: true,
      enableQuic: true,
      header: header,
      method: method,
      responseType: responseType,
      timeout: 0,
      success: res => {
        this.setData({
          recruit_count:res.data.recruit_count
        })
      },
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let user_id = wx.getStorageSync('user_id')
    let windowWidth = wx.getSystemInfoSync().windowWidth
    let page_no = 0
    let status = 2
    this.setData({
      user_id:user_id,
      page_no:page_no,
      status:status,
      windowWidth:windowWidth,
    })

    this.load_recruit_list(status,page_no,false)
    this.uncheck_recruit_count()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(this.data.can_onshow){
      let page_no = 0
      this.load_recruit_list(this.data.status,page_no,false)
      this.uncheck_recruit_count()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(!this.data.no_more){
      this.load_recruit_list(this.data.status,this.data.page_no,true)
    }
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})